namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Globalization;
    using System.Threading;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Routing;
    using Xunit;

    public sealed class TenantRootControllerFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TenantRootController>()
                .DerivesFrom<Controller>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .Implements<IRegisterRoutes>()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new TenantRootController());
        }

        [Fact]
        public void op_Register_RouteCollection()
        {
            RouteCollection routes = new RouteCollection();

            (new TenantRootController() as IRegisterRoutes).Register(routes);

            Assert.Equal<int>(2, routes.Count);

            Assert.Equal<string>("{tenant}", (routes[0] as Route).Url);
            Assert.Equal<string>("{tenant}", (routes[1] as Route).Url);
        }

        [Fact]
        public void op_Register_RouteCollectionNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new TenantRootController() as IRegisterRoutes).Register(null as RouteCollection));
        }

        [Fact]
        public void op_SeeOther()
        {
            FakeRepository.Clear();

            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test.en.html").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                SeeOtherResult result = new TenantRootController().SeeOther() as SeeOtherResult;

                Assert.Equal<string>("/sample/home", result.Location);

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}